package com.hmall.search.mq;

import com.hmall.common.mq.Constants;
import com.hmall.search.service.ISearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ItemListener {

    @Autowired
    private ISearchService searchService;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = Constants.UP_QUEUE),
            exchange = @Exchange(name = Constants.UP_OR_DOWN_TOPIC, type = ExchangeTypes.TOPIC),
            key = Constants.UP_ROUTING_KEY
    ))
    public void itemUp(Long id) {
        searchService.saveItemById(id);
    }

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = Constants.DOWN_QUEUE),
            exchange = @Exchange(name = Constants.UP_OR_DOWN_TOPIC, type = ExchangeTypes.TOPIC),
            key = Constants.DOWN_ROUTING_KEY
    ))
    public void itemDown(Long id) {
        searchService.deleteItemById(id);
    }
}
